#include "adminwidget.h"            // 后台管理
#include "ui_adminwidget.h"
#include<QTreeView>
#include"src/mes.h"
#include<QTableWidget>
#include<QSqlTableModel>
#include<QAbstractItemView>
#include<QMessageBox>
#include<QSqlError>
#include<QMenu>
#include<QStyledItemDelegate>
#include<QDebug>
#include<QStandardItemModel>
#include<QSqlQuery>
#include<QSqlRecord>

//MySqlTableModel::MySqlTableModel(QObject *parent,QSqlDatabase db):
//    QSqlTableModel(parent,db)
//{

//}

//QString MySqlTableModel::selectStatement() const
//{
////     return "SELECT Row_Number(),[name],[roleMap] FROM [dm-data].[dbo].[role]";
////    qDebug()<<"sql";
//    return "select [name],[roleMap] FROM [dm-data].[dbo].[role]";
//}

AdminWidget::AdminWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AdminWidget)
{
    ui->setupUi(this);
    popMenu=new QMenu(ui->classifyView);
    popMenu->addAction("新增分类",this,SLOT(slot_addRow()));
    popMenu->addAction("删除分类",this,SLOT(slot_deleteRow()));
    connect(ui->classifyView,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(slotContextMenu(QPoint)));

    popMenu2=new QMenu(ui->tableView_2);
    popMenu2->addAction("新增分类",this,SLOT(slot_addRow2()));
    popMenu2->addAction("删除分类",this,SLOT(slot_deleteRow2()));
    connect(ui->tableView_2,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(slotContextMenu2(QPoint)));

    popMenu3=new QMenu(ui->tableView_3);
    popMenu3->addAction("新增分类",this,SLOT(slot_addRow3()));
    popMenu3->addAction("删除分类",this,SLOT(slot_deleteRow3()));
    connect(ui->tableView_3,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(slotContextMenu3(QPoint)));

    popMenu4=new QMenu(ui->tableView_4);
    popMenu4->addAction("新增分类",this,SLOT(slot_addRow4()));
    popMenu4->addAction("删除分类",this,SLOT(slot_deleteRow4()));
    connect(ui->tableView_4,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(slotContextMenu4(QPoint)));

    popMenu5=new QMenu(ui->tableView_5);
    popMenu5->addAction("新增分类",this,SLOT(slot_addRow5()));
    popMenu5->addAction("删除分类",this,SLOT(slot_deleteRow5()));
    connect(ui->tableView_5,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(slotContextMenu5(QPoint)));

    popMenu6=new QMenu(ui->tableView_6);
    popMenu6->addAction("新增分类",this,SLOT(slot_addRow6()));
    popMenu6->addAction("删除分类",this,SLOT(slot_deleteRow6()));
    connect(ui->tableView_6,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(slotContextMenu6(QPoint)));

    initClassifyView();
}

bool AdminWidget::initClassifyView()
{
    QSqlTableModel *classifyModel=new QSqlTableModel(this,Mes::getInstance()->db);
    classifyModel->setTable("classificationForm");
    classifyModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    classifyModel->select();
//    classifyModel->setHeaderData(0,Qt::Horizontal,"id");
    classifyModel->setHeaderData(0,Qt::Horizontal,"*分类名字");
    classifyModel->setHeaderData(1,Qt::Horizontal,"*分类编号");
//    classifyModel->setHeaderData(2,Qt::Horizontal,"点检项目");
//    classifyModel->sort(0,Qt::AscendingOrder);
    ui->classifyView->setModel(classifyModel);
    ui->classifyView->setEditTriggers(QAbstractItemView::DoubleClicked);
    ui->classifyView->setSelectionMode(QAbstractItemView::SingleSelection);
//    ui->classifyView->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->classifyView->horizontalHeader()->setSectionsClickable(true);
    ui->classifyView->horizontalHeader()->setHighlightSections(false);
    ui->classifyView->setContextMenuPolicy(Qt::CustomContextMenu);      // 菜单策略
    ui->classifyView->horizontalHeader()->setBackgroundRole(QPalette::Background);
    connect(ui->classifyView->horizontalHeader(),SIGNAL(sectionClicked(int)),this,SLOT(classifySortByColumn(int)));

//    popMenu=new QMenu(ui->classifyView);
////        QAction *actionUpdateInfo=new QAction();
////        actionUpdateInfo->setText("修改");
//    popMenu->addAction("新增分类",this,SLOT(slot_addRow()));
//    popMenu->addAction("删除分类",this,SLOT(slot_deleteRow()));
    return true;
}

void AdminWidget::slot_deleteRow()
{
    QSqlTableModel *classifyModel = dynamic_cast<QSqlTableModel *>(ui->classifyView->model());
    classifyModel->removeRow(selectCurrentRow);
//    classifyModel->submitAll();
    ui->classifyView->reset();
}

bool AdminWidget::initCheckTable()
{
    QSqlTableModel *model=new QSqlTableModel(this,Mes::getInstance()->db);
    model->setTable("checkIndex");
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->select();
    model->setHeaderData(0,Qt::Horizontal,"点检项目");
    ui->tableView_2->setModel(model);
    ui->tableView_2->setEditTriggers(QAbstractItemView::DoubleClicked);
    ui->tableView_2->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableView_2->horizontalHeader()->setSectionsClickable(true);
    ui->tableView_2->horizontalHeader()->setHighlightSections(false);
    ui->tableView_2->setContextMenuPolicy(Qt::CustomContextMenu);      // 菜单策略
    ui->tableView_2->horizontalHeader()->setBackgroundRole(QPalette::Background);
    ui->tableView_2->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    return true;
}

void AdminWidget::slot_addRow2()
{
    QSqlTableModel *model = dynamic_cast<QSqlTableModel *>(ui->tableView_2->model());
    QSqlRecord record = model->record();
    model->insertRecord(model->rowCount(), record);
}

void AdminWidget::slot_deleteRow2()
{
    QSqlTableModel *model = dynamic_cast<QSqlTableModel *>(ui->tableView_2->model());
    model->removeRow(selectCurrentRow);
    ui->tableView_2->reset();
}

void AdminWidget::slotContextMenu2(QPoint pos)
{
    auto index=ui->tableView_2->indexAt(pos);
    selectCurrentRow=index.row();
    if(index.isValid()){
        popMenu2->exec(QCursor::pos());
    }
}

bool AdminWidget::initReCheckTable()
{
    QSqlTableModel *model=new QSqlTableModel(this,Mes::getInstance()->db);
    model->setTable("recheckIndex");
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->select();
    model->setHeaderData(0,Qt::Horizontal,"校验项目");
    ui->tableView_3->setModel(model);
    ui->tableView_3->setEditTriggers(QAbstractItemView::DoubleClicked);
    ui->tableView_3->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableView_3->horizontalHeader()->setSectionsClickable(true);
    ui->tableView_3->horizontalHeader()->setHighlightSections(false);
    ui->tableView_3->setContextMenuPolicy(Qt::CustomContextMenu);      // 菜单策略
    ui->tableView_3->horizontalHeader()->setBackgroundRole(QPalette::Background);
    ui->tableView_3->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    return true;
}

void AdminWidget::slot_addRow3()
{
    QSqlTableModel *model = dynamic_cast<QSqlTableModel *>(ui->tableView_3->model());
    QSqlRecord record = model->record();
    model->insertRecord(model->rowCount(), record);
}

void AdminWidget::slot_deleteRow3()
{
    QSqlTableModel *model = dynamic_cast<QSqlTableModel *>(ui->tableView_3->model());
    model->removeRow(selectCurrentRow);
    ui->tableView_3->reset();
}

void AdminWidget::slotContextMenu3(QPoint pos)
{
    auto index=ui->tableView_3->indexAt(pos);
    selectCurrentRow=index.row();
    if(index.isValid()){
        popMenu3->exec(QCursor::pos());
    }
}

void AdminWidget::on_pushButton_4_clicked()
{
    QSqlTableModel *pModel=dynamic_cast<QSqlTableModel *>(ui->tableView_3->model());
    pModel->database().transaction();
    if(pModel->submitAll()){
        pModel->database().commit();
        QMessageBox::information(NULL,QString("提示"),QString("提交成功        "));
    }else{
        pModel->database().rollback();
        QMessageBox::warning(NULL, QString("错误"), QString("数据库连接失败\n")+pModel->lastError().text());
    }
    pModel->revertAll();
    initReCheckTable();
}

bool AdminWidget::initMaintainTable()
{
    QSqlTableModel *model=new QSqlTableModel(this,Mes::getInstance()->db);
    model->setTable("maintainIndex");
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->select();
    model->setHeaderData(0,Qt::Horizontal,"保养项目");
    ui->tableView_4->setModel(model);
    ui->tableView_4->setEditTriggers(QAbstractItemView::DoubleClicked);
    ui->tableView_4->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableView_4->horizontalHeader()->setSectionsClickable(true);
    ui->tableView_4->horizontalHeader()->setHighlightSections(false);
    ui->tableView_4->setContextMenuPolicy(Qt::CustomContextMenu);      // 菜单策略
    ui->tableView_4->horizontalHeader()->setBackgroundRole(QPalette::Background);
    ui->tableView_4->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    return true;
}

void AdminWidget::slot_addRow4()
{
    QSqlTableModel *model = dynamic_cast<QSqlTableModel *>(ui->tableView_4->model());
    QSqlRecord record = model->record();
    model->insertRecord(model->rowCount(), record);
}

void AdminWidget::slot_deleteRow4()
{
    QSqlTableModel *model = dynamic_cast<QSqlTableModel *>(ui->tableView_4->model());
    model->removeRow(selectCurrentRow);
    ui->tableView_4->reset();
}

void AdminWidget::slotContextMenu4(QPoint pos)
{
    auto index=ui->tableView_4->indexAt(pos);
    selectCurrentRow=index.row();
    if(index.isValid()){
        popMenu4->exec(QCursor::pos());
    }
}

void AdminWidget::on_pushButton_5_clicked()
{
    QSqlTableModel *pModel=dynamic_cast<QSqlTableModel *>(ui->tableView_4->model());
    pModel->database().transaction();
    if(pModel->submitAll()){
        pModel->database().commit();
        QMessageBox::information(NULL,QString("提示"),QString("提交成功        "));
    }else{
        pModel->database().rollback();
        QMessageBox::warning(NULL, QString("错误"), QString("数据库连接失败\n")+pModel->lastError().text());
    }
    pModel->revertAll();
    initMaintainTable();
}

bool AdminWidget::initLocationTable()
{
    QSqlTableModel *model=new QSqlTableModel(this,Mes::getInstance()->db);
    model->setTable("locationIndex");
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->select();
    model->setHeaderData(0,Qt::Horizontal,"存放地点");
    ui->tableView_5->setModel(model);
    ui->tableView_5->setEditTriggers(QAbstractItemView::DoubleClicked);
    ui->tableView_5->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableView_5->horizontalHeader()->setSectionsClickable(true);
    ui->tableView_5->horizontalHeader()->setHighlightSections(false);
    ui->tableView_5->setContextMenuPolicy(Qt::CustomContextMenu);      // 菜单策略
    ui->tableView_5->horizontalHeader()->setBackgroundRole(QPalette::Background);
//    ui->tableView_5->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    return true;
}

void AdminWidget::slot_addRow5()
{
    QSqlTableModel *model = dynamic_cast<QSqlTableModel *>(ui->tableView_5->model());
    QSqlRecord record = model->record();
    model->insertRecord(model->rowCount(), record);
}

void AdminWidget::slot_deleteRow5()
{
    QSqlTableModel *model = dynamic_cast<QSqlTableModel *>(ui->tableView_5->model());
    model->removeRow(selectCurrentRow);
    ui->tableView_5->reset();
}

void AdminWidget::slotContextMenu5(QPoint pos)
{
    auto index=ui->tableView_5->indexAt(pos);
    selectCurrentRow=index.row();
    if(index.isValid()){
        popMenu5->exec(QCursor::pos());
    }
}

void AdminWidget::on_pushButton_6_clicked()
{
    QSqlTableModel *pModel=dynamic_cast<QSqlTableModel *>(ui->tableView_5->model());
    pModel->database().transaction();
    if(pModel->submitAll()){
        pModel->database().commit();
        QMessageBox::information(NULL,QString("提示"),QString("提交成功        "));
    }else{
        pModel->database().rollback();
        QMessageBox::warning(NULL, QString("错误"), QString("数据库连接失败\n")+pModel->lastError().text());
    }
    pModel->revertAll();
    initLocationTable();
}

bool AdminWidget::initRestTable()
{
    QSqlTableModel *model=new QSqlTableModel(this,Mes::getInstance()->db);
    model->setTable("restDate");
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->select();
    model->setHeaderData(0,Qt::Horizontal,"跳过休息日");
    ui->tableView_6->setModel(model);
    ui->tableView_6->setEditTriggers(QAbstractItemView::DoubleClicked);
    ui->tableView_6->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableView_6->horizontalHeader()->setSectionsClickable(true);
    ui->tableView_6->horizontalHeader()->setHighlightSections(false);
    ui->tableView_6->setContextMenuPolicy(Qt::CustomContextMenu);      // 菜单策略
    ui->tableView_6->horizontalHeader()->setBackgroundRole(QPalette::Background);
//    ui->tableView_6->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    return true;
}

void AdminWidget::slot_addRow6()
{
    QSqlTableModel *model = dynamic_cast<QSqlTableModel *>(ui->tableView_6->model());
    QSqlRecord record = model->record();
    model->insertRecord(model->rowCount(), record);
}

void AdminWidget::slot_deleteRow6()
{
    QSqlTableModel *model = dynamic_cast<QSqlTableModel *>(ui->tableView_6->model());
    model->removeRow(selectCurrentRow);
    ui->tableView_6->reset();
}

void AdminWidget::slotContextMenu6(QPoint pos)
{
    auto index=ui->tableView_6->indexAt(pos);
    selectCurrentRow=index.row();
    if(index.isValid()){
        popMenu6->exec(QCursor::pos());
    }
}

void AdminWidget::on_pushButton_7_clicked()
{
    QSqlTableModel *pModel=dynamic_cast<QSqlTableModel *>(ui->tableView_6->model());
    pModel->database().transaction();
    if(pModel->submitAll()){
        pModel->database().commit();
        QMessageBox::information(NULL,QString("提示"),QString("提交成功        "));
    }else{
        pModel->database().rollback();
        QMessageBox::warning(NULL, QString("错误"), QString("数据库连接失败\n")+pModel->lastError().text());
    }
    pModel->revertAll();
    initRestTable();
}

void AdminWidget::on_pushButton_3_clicked()
{
    QSqlTableModel *pModel=dynamic_cast<QSqlTableModel *>(ui->tableView_2->model());
    pModel->database().transaction();
    if(pModel->submitAll()){
        pModel->database().commit();
        QMessageBox::information(NULL,QString("提示"),QString("提交成功        "));
    }else{
        pModel->database().rollback();
        QMessageBox::warning(NULL, QString("错误"), QString("数据库连接失败\n")+pModel->lastError().text());
    }
    pModel->revertAll();
    initCheckTable();
}

void AdminWidget::slotContextMenu(QPoint pos)
{
    auto index=ui->classifyView->indexAt(pos);
    selectCurrentRow=index.row();
    if(index.isValid()){
        popMenu->exec(QCursor::pos());
    }
}

void AdminWidget::slot_addRow()
{
    QSqlTableModel *classifyModel = dynamic_cast<QSqlTableModel *>(ui->classifyView->model());
    QSqlRecord record = classifyModel->record();
//    int number;
//    // 从1开始遍历，遇到相同的，自增再遍历，直到没有相同的作为number插入
//    // 防止唯一主键重复导致提交失败，因为提交失败tableview还是会更新修改后的（视图）
//    for(number = 1; ; number++)
//    {
//        bool bFlag = false;
//        for(int index = 0; index < classifyModel->rowCount(); index++) {
//            if(classifyModel->index(index, 0).data().toInt() == number) {
//                bFlag = true;
//                break;
//            }
//        }
//        if(!bFlag) {
//            break;
//        }
//    }
//    record.setValue(0, number);
//    record.setValue(1, "");
//    record.setValue(2, "");
//    record.setValue(3, "");
    classifyModel->insertRecord(classifyModel->rowCount(), record);
    // 每次手动提交，都会重新刷新tableView，保持mode和tableView一致
//    classifyModel->submitAll();
//    ui->classifyView->reset();
}

void AdminWidget::classifySortByColumn(int col)
{
    QSqlTableModel *classifyModel=dynamic_cast<QSqlTableModel *>(ui->classifyView->model());
    bool ascending=(ui->classifyView->horizontalHeader()->sortIndicatorSection()==col
                    && ui->classifyView->horizontalHeader()->sortIndicatorOrder()==Qt::DescendingOrder);
    Qt::SortOrder order=ascending ? Qt::AscendingOrder : Qt::DescendingOrder;
    classifyModel->sort(col,order);
}

void AdminWidget::on_pushButton_2_clicked()
{
    QSqlTableModel *classifyModel = dynamic_cast<QSqlTableModel *>(ui->classifyView->model());
    classifyModel->database().transaction(); //开始事务操作
    if (classifyModel->submitAll()) // 提交所有被修改的数据到数据库中
    {
        classifyModel->database().commit(); //提交成功，事务将真正修改数据库数据
        QMessageBox::information(NULL,QString("提示"),QString("提交成功        "));
    } else {
        classifyModel->database().rollback(); //提交失败，事务回滚
        QMessageBox::warning(NULL, QString("错误"), QString("数据库连接失败\n")+classifyModel->lastError().text());
    }
    classifyModel->revertAll(); //撤销修改
    initClassifyView();
}

void AdminWidget::LoadTree()        //加载树控件
{
    QSqlQuery query;

//    QString dtype = NameTypeMap[ui->comboBox->currentText()];    //映射出type
//    int i = ui->comboBox->currentIndex();
//    QString dtype = QString::number(i+1,10);
    QString sql = QString("SELECT * FROM ATT_DIC_TREE where TYPE = '%1' order by SJID,DLID;").arg("");
    query.exec(sql);
    //如下开始从QMap中的数据加载到树控件上
    while(query.next())
    {
        QMap<int,QStandardItem*> mMapItems;

        while(query.next())
        {
            int dlid = query.value(2).toInt();
            int sjid = query.value(3).toInt();
            QString name = query.value(4).toString();
            if(sjid==0 || mMapItems.constFind(sjid)==mMapItems.constEnd())
            {
                QStandardItem *itemProject2 = new QStandardItem(name);
                itemProject2->setData(name);
                model->appendRow(itemProject2);

                mMapItems[dlid]=itemProject2;
            }
            else if(mMapItems.constFind(sjid)!=mMapItems.constEnd())
            {
                QStandardItem* pPar=mMapItems[sjid];
                QStandardItem* item0 = new QStandardItem(name);
                item0->setData(name);
                pPar->appendRow(item0);

                mMapItems[dlid]=item0;
            }
            if(name.isEmpty())
                break;
        }
        break;
    }

    QString NameTypeSql = QString("SELECT * FROM ATT_DIC_TREE where type = '%1' and sjid = 0").arg("dtype");
        query.exec(NameTypeSql);
    while(query.next())
        name = query.value(0).toString();

    ui->classifyView->setModel(model);
}

bool AdminWidget::initTableView()
{
    QSqlTableModel *pModel=new QSqlTableModel(this,Mes::getInstance()->db);
    pModel->setTable("role");
    pModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    pModel->select();
    pModel->setHeaderData(0,Qt::Horizontal,"编码");
    pModel->setHeaderData(1,Qt::Horizontal,"职位");
    pModel->setHeaderData(2,Qt::Horizontal,"权限");
    ui->tableView->setModel(pModel);
    ui->tableView->setItemDelegate(new ComboDelegateYC);
//    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableView->setEditTriggers(QAbstractItemView::CurrentChanged);
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableView->horizontalHeader()->setSectionsClickable(true);
    ui->tableView->horizontalHeader()->setHighlightSections(false);
    ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
    ui->tableView->horizontalHeader()->setBackgroundRole(QPalette::Background);
//    ui->tableView->horizontalHeader()->setStyleSheet("QHeaderView::section{background-color:rgb(225,225,225)};");
    connect(ui->tableView->horizontalHeader(),SIGNAL(sectionClicked(int)),this,SLOT(soryByColumn(int)));
    return true;
}

void AdminWidget::soryByColumn(int col)
{
    QSqlTableModel *pModel=dynamic_cast<QSqlTableModel *>(ui->tableView->model());
    bool ascending=(ui->tableView->horizontalHeader()->sortIndicatorSection()==col
                    && ui->tableView->horizontalHeader()->sortIndicatorOrder()==Qt::DescendingOrder);
    Qt::SortOrder order=ascending ? Qt::AscendingOrder : Qt::DescendingOrder;
    pModel->sort(col,order);
}

AdminWidget::~AdminWidget()
{
    delete ui;
}

void AdminWidget::on_tabWidget_currentChanged(int index)
{
    switch (index) {
        case 0:initClassifyView();break;
        case 1:initTableView();break;
        case 2:initCheckTable();break;
        case 3:initReCheckTable();break;
        case 4:initMaintainTable();break;
        case 5:initLocationTable();break;
        case 6:initRestTable();break;
    }
}

void AdminWidget::on_pushButton_clicked()
{
   QSqlTableModel *pModel=dynamic_cast<QSqlTableModel *>(ui->tableView->model());
   pModel->database().transaction();
   if(pModel->submitAll()){
       pModel->database().commit();
       QMessageBox::information(NULL,QString("提示"),QString("提交成功        "));
   }else{
       pModel->database().rollback();
       QMessageBox::warning(NULL, QString("错误"), QString("数据库连接失败\n")+pModel->lastError().text());
   }
   pModel->revertAll();
   initTableView();
}



ComboDelegateYC::ComboDelegateYC(QObject *parent) //空的构造函数也是必须的，之前忘了写，编译报错
        : QItemDelegate(parent)
{
}


QWidget* ComboDelegateYC::createEditor(
        QWidget *parent, const QStyleOptionViewItem &option,
        const QModelIndex &index) const
{
    if(index.column() == colRycchannel)   //colRycchannel是列的枚举类型，也可以用立即数，那样比较low
    {
        QComboBox *channelBox = new QComboBox(parent); //此处如果不加parent，edit不会内嵌在表格中 会跑出来
        channelBox->setFixedHeight(option.rect.height());
        QStringList l={"员工","组长","经理","管理员"};
        channelBox->addItems(l);
        return channelBox;
    }
    else
    {
//        return QItemDelegate::createEditor(parent, option, index);
        return NULL;
    }

}

//以下几个函数都可以直接拷贝用
void ComboDelegateYC::setEditorData(QWidget * editor, const QModelIndex & index) const
{
    if(index.column() == colRycchannel)
    {
        QComboBox *comboBox = static_cast<QComboBox*>(editor);
        if(comboBox)
        {
            QString str = index.model()->data(index, Qt::EditRole).toString();
            comboBox->setCurrentIndex(comboBox->findText(str));
        }
    }
    else
    {
        QItemDelegate::setEditorData(editor, index);
    }
}


void ComboDelegateYC::setModelData(QWidget *editor, QAbstractItemModel *model,
                                   const QModelIndex &index) const
{
    int nColumn = index.column();
    if((nColumn == colRycchannel))
    {
        QComboBox *comboBox = static_cast<QComboBox*>(editor);
        if(comboBox!=0)
            model->setData(index, comboBox->currentText(), Qt::EditRole);
    }
    else
    {
        QItemDelegate::setModelData(editor, model, index);
    }
}


//void ComboDelegateYC::updateEditorGeometry(QWidget *editor,
//                                           const QStyleOptionViewItem &option, const QModelIndex &) const
//{
//    editor->setGeometry(option.rect);
//}



